{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Neither Minimum nor Maximum"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #sorting"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #排序"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: findNonMinOrMax"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #既不是最小值也不是最大值"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个整数数组 <code>nums</code> ，数组由 <strong>不同正整数</strong> 组成，请你找出并返回数组中 <strong>任一</strong> 既不是 <strong>最小值</strong> 也不是 <strong>最大值</strong> 的数字，如果不存在这样的数字，返回 <strong><code>-1</code></strong> 。</p>\n",
    "\n",
    "<p>返回所选整数。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>nums = [3,2,1,4]\n",
    "<strong>输出：</strong>2\n",
    "<strong>解释：</strong>在这个示例中，最小值是 1 ，最大值是 4 。因此，2 或 3 都是有效答案。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>nums = [1,2]\n",
    "<strong>输出：</strong>-1\n",
    "<strong>解释：</strong>由于不存在既不是最大值也不是最小值的数字，我们无法选出满足题目给定条件的数字。因此，不存在答案，返回 -1 。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 3：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>nums = [2,1,3]\n",
    "<strong>输出：</strong>2\n",
    "<strong>解释：</strong>2 既不是最小值，也不是最大值，这个示例只有这一个有效答案。 \n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= nums.length &lt;= 100</code></li>\n",
    "\t<li><code>1 &lt;= nums[i] &lt;= 100</code></li>\n",
    "\t<li><code>nums</code> 中的所有数字互不相同</li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [neither-minimum-nor-maximum](https://leetcode.cn/problems/neither-minimum-nor-maximum/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [neither-minimum-nor-maximum](https://leetcode.cn/problems/neither-minimum-nor-maximum/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[3,2,1,4]', '[1,2]', '[2,1,3]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findNonMinOrMax(self, nums: List[int]) -> int:\n",
    "        return -1 if len(nums) <= 2 else sorted(nums)[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findNonMinOrMax(self, nums: List[int]) -> int:\n",
    "        mx,mi = max(nums),min(nums)\n",
    "        if len(nums)<=2:\n",
    "            return -1\n",
    "        for i in range(len(nums)):\n",
    "            if nums[i] not in (mx,mi):\n",
    "                return nums[i]\n",
    "        return -1\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findNonMinOrMax(self, nums: List[int]) -> int:\n",
    "        max_num = max(nums)\n",
    "        min_num = min(nums)\n",
    "        for i in nums:\n",
    "            if i != max_num and i != min_num:\n",
    "                return i\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findNonMinOrMax(self, nums: List[int]) -> int:\n",
    "        if len(nums) < 3:\n",
    "            return -1\n",
    "        return sorted(nums[:3])[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findNonMinOrMax(self, nums: List[int]) -> int:\n",
    "        _max = max(nums)\n",
    "        _min = min(nums)\n",
    "        for i in nums:\n",
    "            if i != _max and i != _min:\n",
    "                return i\n",
    "        return -1;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findNonMinOrMax(self, nums: List[int]) -> int:\n",
    "        if len(nums)<3:\n",
    "            return -1\n",
    "        return sorted(nums[:3])[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findNonMinOrMax(self, nums: List[int]) -> int:\n",
    "        minium = min(nums)\n",
    "        maximam = max(nums)\n",
    "        for num in nums:\n",
    "            if num != minium and num != maximam:\n",
    "                return num\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findNonMinOrMax(self, nums: List[int]) -> int:\n",
    "        if len(nums)<=2: return -1\n",
    "        return sorted(nums)[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findNonMinOrMax(self, nums: List[int]) -> int:\n",
    "        if len(nums) <= 2:\n",
    "            return -1\n",
    "        nums.sort()\n",
    "        return nums[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findNonMinOrMax(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        return -1 if len(nums) < 3 else nums[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findNonMinOrMax(self, nums: List[int]) -> int:\n",
    "        if len(nums)>2:\n",
    "            return sorted(nums[0:3])[1]\n",
    "        else:\n",
    "            return -1\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findNonMinOrMax(self, nums: List[int]) -> int:\n",
    "        minimum, maximum = min(nums), max(nums)\n",
    "        for num in nums:\n",
    "            if minimum < num < maximum:\n",
    "                return num\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findNonMinOrMax(self, nums: List[int]) -> int:\n",
    "        return -1 if len(nums) <= 2 else sorted(nums[:3])[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findNonMinOrMax(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        return nums[1] if len(nums)>2 else -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findNonMinOrMax(self, nums: List[int]) -> int:\n",
    "        if len(nums)>2:\n",
    "            return sorted(nums[0:3])[1]\n",
    "        else:\n",
    "            return -1\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findNonMinOrMax(self, nums: List[int]) -> int:\n",
    "        mx=max(nums)\n",
    "        mn=min(nums)\n",
    "        for x in nums:\n",
    "            if x!=mx and x!=mn:\n",
    "                return x\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    import random\n",
    "    def findNonMinOrMax(self, nums: List[int]) -> int:\n",
    "        if len(nums)<=2:\n",
    "            return -1\n",
    "        else:\n",
    "            for i in range(len(nums)):\n",
    "                if nums[i] not in {max(nums),min(nums)}:\n",
    "                    return nums[i]\n",
    "               "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findNonMinOrMax(self, nums: List[int]) -> int:\n",
    "        if len(nums) <= 2:\n",
    "            return -1\n",
    "        else:\n",
    "            nums.sort()\n",
    "            return nums[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findNonMinOrMax(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        if n <= 2:\n",
    "            return -1\n",
    "        return sorted(nums)[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findNonMinOrMax(self, nums: List[int]) -> int:\n",
    "       #mx=max(nums)\n",
    "       #mn=min(nums)\n",
    "       #for x in nums:\n",
    "       #    if x!=mx and x!=mn:\n",
    "       #        return x\n",
    "       #return -1\n",
    "       return sorted(nums[:3])[1] if len(nums) > 2 else -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findNonMinOrMax(self, nums: List[int]) -> int:\n",
    "        return sorted(nums[:3])[1] if len(nums) > 2 else -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findNonMinOrMax(self, nums: List[int]) -> int:\n",
    "        ma = max(nums)\n",
    "        mi = min(nums)\n",
    "        for i in nums:\n",
    "            if i != mi and i != ma:\n",
    "                return i\n",
    "        return -1\n",
    "'''2733. 既不是最小值也不是最大值\n",
    "给你一个整数数组 nums ，数组由 不同正整数 组成，请你找出并返回数组中 任一 既不是 最小值 也不是 最大值 的数字，如果不存在这样的数字，返回 -1 。\n",
    "返回所选整数。\n",
    "示例 1：\n",
    "输入：nums = [3,2,1,4]\n",
    "输出：2\n",
    "解释：在这个示例中，最小值是 1 ，最大值是 4 。因此，2 或 3 都是有效答案。\n",
    "示例 2：\n",
    "输入：nums = [1,2]\n",
    "输出：-1\n",
    "解释：由于不存在既不是最大值也不是最小值的数字，我们无法选出满足题目给定条件的数字。因此，不存在答案，返回 -1 。\n",
    "示例 3：\n",
    "输入：nums = [2,1,3]\n",
    "输出：2\n",
    "解释：2 既不是最小值，也不是最大值，这个示例只有这一个有效答案。 \n",
    "提示：\n",
    "1 <= nums.length <= 100\n",
    "1 <= nums[i] <= 100\n",
    "nums 中的所有数字互不相同\n",
    "通过次数\n",
    "8.8K\n",
    "提交次数\n",
    "11.3K\n",
    "通过率\n",
    "77.7%'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findNonMinOrMax(self, nums: List[int]) -> int:\n",
    "        m,n = max(nums),min(nums)\n",
    "        for x in nums:\n",
    "            if x != m and x != n:\n",
    "                return x\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findNonMinOrMax(self, nums: List[int]) -> int:\n",
    "        if len(nums) < 3:\n",
    "            return -1\n",
    "        \n",
    "        min_val = min(nums)\n",
    "        max_val = max(nums)\n",
    "\n",
    "        # 移除最大值和最小值\n",
    "        nums.remove(min_val)\n",
    "        nums.remove(max_val)\n",
    "\n",
    "        # 现在 nums 中的任何一个元素都既不是最小值也不是最大值\n",
    "        # 返回 nums 中的一个元素\n",
    "        return nums[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findNonMinOrMax(self, nums: List[int]) -> int:\n",
    "        if len(nums) < 3:\n",
    "            return -1\n",
    "        else:\n",
    "            nums.sort()\n",
    "            nums.pop(0)\n",
    "            nums.pop(-1)\n",
    "            return nums[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findNonMinOrMax(self, nums: List[int]) -> int:\n",
    "        if len(nums)<3:\n",
    "            return -1\n",
    "        nums.sort()\n",
    "        return nums[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findNonMinOrMax(self, nums: List[int]) -> int:\n",
    "        if len(nums) <= 2:\n",
    "            return -1\n",
    "        else:\n",
    "            nums.remove(max(nums))\n",
    "            nums.remove(min(nums))\n",
    "            return nums[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findNonMinOrMax(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        return -1 if len(nums) <= 2 else nums[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findNonMinOrMax(self, nums: List[int]) -> int:\n",
    "        l = len(nums)\n",
    "        if l <= 2:\n",
    "            return -1\n",
    "        nums.sort()\n",
    "        return nums[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findNonMinOrMax(self, nums: List[int]) -> int:\n",
    "        return sorted(nums[:3])[1] if len(nums) > 2 else -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findNonMinOrMax(self, nums: List[int]) -> int:\n",
    "        if len(nums) <= 2:\n",
    "            return -1\n",
    "        a, b, c = nums[:3]\n",
    "        if a > b:\n",
    "            a, b = b, a\n",
    "        if c < a:\n",
    "            return a\n",
    "        elif c > b:\n",
    "            return b\n",
    "        return c\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findNonMinOrMax(self, nums: List[int]) -> int:\n",
    "        return sorted(nums[:3])[1] if len(nums) > 2 else -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findNonMinOrMax(self, nums: List[int]) -> int:\n",
    "        flag_1 = max(nums)\n",
    "        flag_2 = min(nums)\n",
    "        for num in nums:\n",
    "            if num != flag_1 and num != flag_2:\n",
    "                return num\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findNonMinOrMax(self, nums: List[int]) -> int:\n",
    "        return sorted(nums[:3])[1] if len(nums) > 2 else -1\n",
    "        # n = len(nums)\n",
    "        # if n<=2:\n",
    "        #     return -1\n",
    "        # nums.sort()\n",
    "        # return nums[n//2]\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findNonMinOrMax(self, nums: List[int]) -> int:\n",
    "        if len(nums) < 3:\n",
    "            return -1\n",
    "        return sorted(nums[:3])[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findNonMinOrMax(self, nums: List[int]) -> int:\n",
    "        return sorted(nums)[1] if len(nums) > 2 else -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findNonMinOrMax(self, nums: List[int]) -> int:\n",
    "        if len(set(nums))==2 or len(set(nums))==1:\n",
    "            return -1\n",
    "        else:\n",
    "            return sorted(set(nums))[-2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findNonMinOrMax(self, nums: List[int]) -> int:\n",
    "        # if len(nums) <= 2:\n",
    "        #     return -1\n",
    "        # nums.sort()\n",
    "        # return nums[1]\n",
    "        \n",
    "        return sorted(nums[:3])[1] if len(nums) > 2 else -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findNonMinOrMax(self, nums: List[int]) -> int:\n",
    "        return sorted(nums[:3])[1] if len(nums) > 2 else -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findNonMinOrMax(self, nums: List[int]) -> int:\n",
    "        if len(nums) <= 2:\n",
    "            return -1\n",
    "        max1 = nums[0]\n",
    "        min1 = nums[0]\n",
    "        for i in range(len(nums)):\n",
    "            if nums[i] < min1:\n",
    "                min1 = nums[i]\n",
    "            elif nums[i] > max1:\n",
    "                max1 = nums[i]\n",
    "            print\n",
    "        for i in nums:\n",
    "            if i != max1 and i != min1:\n",
    "                return i\n",
    "        # return -1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findNonMinOrMax(self, nums: List[int]) -> int:\n",
    "        return sorted(nums[:3])[1] if len(nums) > 2 else -1\n",
    "\n",
    "\n",
    "   "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findNonMinOrMax(self, nums: List[int]) -> int:\n",
    "        return sorted(nums)[1] if len(nums)>2 else -1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findNonMinOrMax(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        n = len(nums)\n",
    "        if n <= 2:\n",
    "            return -1\n",
    "        v1 = nums[n//2]\n",
    "        v2 = nums[n//2 - 1]\n",
    "        if nums[0] < v1 < nums[n-1]:\n",
    "            return v1\n",
    "        if nums[0] < v2 < nums[n-1]:\n",
    "            return v2\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findNonMinOrMax(self, nums: List[int]) -> int:\n",
    "        if len(nums) == 1 or len(nums) == 2:\n",
    "            return -1\n",
    "        mins = min(nums)\n",
    "        maxs = max(nums)\n",
    "        ans = -1\n",
    "        for i in nums:\n",
    "            if i != mins and i != maxs:\n",
    "                ans = i\n",
    "                break\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findNonMinOrMax(self, nums: List[int]) -> int:\n",
    "       \n",
    "        nums.sort()\n",
    "        # nums=set(nums)\n",
    "        # nums=list(nums)\n",
    "        if len(nums)<=2:\n",
    "            return -1\n",
    "        else:\n",
    "            return nums[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findNonMinOrMax(self, nums: List[int]) -> int:\n",
    "        if len(nums)>2:\n",
    "            del nums[nums.index(max(nums))],nums[nums.index(min(nums))]\n",
    "            return nums[0]\n",
    "        else:return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findNonMinOrMax(self, nums: List[int]) -> int:\n",
    "        if len(nums) <= 2:\n",
    "            return -1\n",
    "        a, b, c = nums[:3]\n",
    "        if a > b:\n",
    "            a, b = b, a\n",
    "        if c < a:\n",
    "            return a\n",
    "        elif c > b:\n",
    "            return b\n",
    "        return c\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findNonMinOrMax(self, nums: List[int]) -> int:\n",
    "        if len(nums) < 3:\n",
    "            return -1\n",
    "        nums.remove(max(nums))\n",
    "        nums.remove(min(nums))\n",
    "        return nums[0]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findNonMinOrMax(self, nums: List[int]) -> int:\n",
    "        _min = min(nums)\n",
    "        _max = max(nums)    \n",
    "        for i in nums:\n",
    "            if i != _min and i!=_max:\n",
    "                return i\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findNonMinOrMax(self, nums: List[int]) -> int:\n",
    "        if len(nums) <= 2:\n",
    "            return -1\n",
    "        else:\n",
    "            nums.remove(max(nums))\n",
    "            nums.remove(min(nums))\n",
    "            return random.choice(nums)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution(object):\n",
    "    def findNonMinOrMax(self, nums):\n",
    "        \"\"\"\n",
    "        :type nums: List[int]\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        m = max(nums)\n",
    "        n = min(nums)\n",
    "        for i in nums:\n",
    "            if i != m and i != n:\n",
    "                return i\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findNonMinOrMax(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        n = len(nums)\n",
    "        if n <= 2:\n",
    "            return -1\n",
    "        return nums[n//2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findNonMinOrMax(self, nums: List[int]) -> int:\n",
    "        if len(nums)<3:\n",
    "            return -1\n",
    "        for i in nums:\n",
    "            if i!=max(nums) and i!=min(nums):\n",
    "                return i"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findNonMinOrMax(self, nums: List[int]) -> int:\n",
    "        if len(nums) <= 2:\n",
    "            return -1\n",
    "        nums.sort()\n",
    "        return nums[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findNonMinOrMax(self, nums: List[int]) -> int:\n",
    "        return sorted(nums[:3])[1] if len(nums) > 2 else -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findNonMinOrMax(self, nums: List[int]) -> int:\n",
    "        if len(nums) <=2: return -1\n",
    "        return sorted(nums[:3])[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findNonMinOrMax(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        if n<=2:\n",
    "            return -1\n",
    "        nums.sort()\n",
    "        return nums[n//2]\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findNonMinOrMax(self, nums: List[int]) -> int:\n",
    "        if(len(nums) <= 2):\n",
    "            return -1\n",
    "        return max(min(nums[0], nums[1]), min(nums[1], nums[2]), min(nums[0], nums[2]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findNonMinOrMax(self, nums: List[int]) -> int:\n",
    "        l1 = set(nums)\n",
    "        if len(l1) <= 2:\n",
    "            return -1\n",
    "        else:\n",
    "            x = min(nums[0], nums[1])\n",
    "            y = max(nums[0], nums[1])\n",
    "            for i in range (2, len(nums)):\n",
    "                if nums[i] > y:\n",
    "                    return y\n",
    "                elif nums[i] < x:\n",
    "                    return x\n",
    "                else:\n",
    "                    if nums[i] < y and x < nums[i]:\n",
    "                        return nums[i]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findNonMinOrMax(self, nums: List[int]) -> int:\n",
    "        nmin=min(nums)\n",
    "        nmax=max(nums)\n",
    "        for num in nums:\n",
    "            if num!=nmin and num!=nmax:\n",
    "                return num\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findNonMinOrMax(self, nums: List[int]) -> int:\n",
    "        return next(iter(set(nums) - set([min(nums), max(nums)])), -1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findNonMinOrMax(self, nums: List[int]) -> int:\n",
    "        if len(nums) <= 2:\n",
    "            return -1\n",
    "        else:\n",
    "            nums.sort()\n",
    "            return nums[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findNonMinOrMax(self, nums: List[int]) -> int:\n",
    "        maxNum,minNum = max(nums),min(nums)\n",
    "        for num in nums:\n",
    "            if num!=maxNum and num!=minNum:\n",
    "                return num\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findNonMinOrMax(self, nums: List[int]) -> int:\n",
    "        if len(nums)<=2:\n",
    "            return -1\n",
    "        else:\n",
    "           nums.sort()\n",
    "           return nums[1]\n",
    "        \n",
    "\n",
    "\n",
    "         "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findNonMinOrMax(self, nums: List[int]) -> int:\n",
    "        if len(nums) == 1 or len(nums) == 2:\n",
    "            return -1\n",
    "        nums = sorted(nums)\n",
    "        return nums[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findNonMinOrMax(self, nums: List[int]) -> int:\n",
    "        if (len(nums) < 3):\n",
    "            return -1\n",
    "        minNum, maxNum = 101, 0\n",
    "        for num in nums:\n",
    "            minNum = min(minNum, num)\n",
    "            maxNum = max(maxNum, num)\n",
    "        for num in nums:\n",
    "            if num != minNum and num != maxNum:\n",
    "                return num\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findNonMinOrMax(self, nums: List[int]) -> int:\n",
    "        if len(nums) <= 2:\n",
    "            return -1\n",
    "        \n",
    "        elif len(nums) > 2:\n",
    "            nums.sort()\n",
    "            return nums[-2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import math\n",
    "class Solution:\n",
    "    def findNonMinOrMax(self, nums: List[int]) -> int:\n",
    "        min = None\n",
    "        max = None\n",
    "        for num in nums:\n",
    "            if min is None:\n",
    "                min = num\n",
    "                max = num\n",
    "            elif num < min:\n",
    "                if min < max:\n",
    "                    return min\n",
    "                else:\n",
    "                    min = num\n",
    "            elif num > max:\n",
    "                if min < max:\n",
    "                    return max\n",
    "                else:\n",
    "                    max = num\n",
    "            elif num > min and num < max:\n",
    "                return num\n",
    "        return -1\n",
    "\n"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
